<template>
  <div class="updateName">
    <!-- 头部导航 -->
    <van-nav-bar
      title="更新昵称"
      left-text="取消"
      right-text="保存"
      left-arrow
      @click-left="onClickLeft"
      @click-right="onClickRight"
    />
    <!-- 文本域 -->
    <van-field
      v-model="nickname"
      rows="2"
      autosize
      type="textarea"
      maxlength="11"
      placeholder="请输入昵称"
      show-word-limit
    />
  </div>
</template>

<script>
import { editUserProfileAPI } from "@/api";
export default {
  name: "updataNickname",
  components: {},
  props: {
    name: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      nickname: this.name
    };
  },
  computed: {},
  watch: {},
  created() {},
  methods: {
    onClickLeft() {
      this.$emit("close");
    },
    async onClickRight() {
      try {
        this.$toast.loading({
          message: "更新昵称中...",
          forbidClick: true
        });
        // 发送请求
        await editUserProfileAPI({
          name: this.nickname
        });

        // 手动更新视图
        // sync：v-bind指令修饰符
        // 用法 ：属性.sync="xxx"
        // 作用：更新绑定的值
        // 实际上绑定了一个自定义事件：update：属性
        this.$emit("update:name", this.nickname);

        // 关闭弹窗
        this.$emit("close");

        // 更新成功
        this.$toast.success("更新昵称成功");
      } catch (error) {
        this.$toast.fail("更新昵称失败");
      }
    }
  }
};
</script>

<style scoped lang="less">
.updateName {
  :deep(.van-nav-bar__content) {
    background-color: #fff;
    .van-nav-bar__title {
      color: #b0b0b1;
    }
    .van-icon-arrow-left {
      color: #b0b0b1;
    }
  }
}
</style>
